package com.a85.wxhelper.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import static com.a85.wxhelper.util.RootUtil.isDeviceRooted;

/**
 * @author hejiangwei
 * @date 2018/3/30
 * 安装与卸载的广播接收者
 * 使用了静态注册
 */

public class AppReceiver extends BroadcastReceiver {

    private static final String TAG = AppReceiver.class.getName();
    @Override
    public void onReceive(Context context, Intent intent) {
        //接收安装广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {
            String data = intent.getDataString();
            String packageName = data.substring(data.indexOf(":") + 1);
            Log.e(TAG, "安装: "+packageName );
        }

        //接收更新广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_REPLACED")) {
            String data = intent.getDataString();
            String packageName = data.substring(data.indexOf(":") + 1);
            Log.e(TAG, "更新: "+packageName );
            if (isDeviceRooted()){
                Log.d(TAG, "onCreate: 你的设备可以获取root");
            }else {
                Log.d(TAG, "onCreate: 你的设备可以获取不能获取root");

            }
        }

        //接收卸载广播
        if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {
            String data = intent.getDataString();
            String packageName = data.substring(data.indexOf(":") + 1);
            Log.e(TAG, "卸载: "+packageName );

        }
    }
}
